package com.ruoyi.operator.service.impl;


import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.operator.domain.OptShopCategory;
import com.ruoyi.operator.domain.OptShopProduct;
import com.ruoyi.operator.mapper.OptShopCategoryMapper;
import com.ruoyi.operator.mapper.OptShopProductMapper;
import com.ruoyi.operator.service.IOptShopProductService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商家商品Service业务层处理
 * @date 2021-02-08
 */
@Service
public class OptShopProductServiceImpl implements IOptShopProductService
{
    @Autowired
    private OptShopProductMapper optShopProductMapper;
    @Autowired
    private OptShopCategoryMapper optShopCategoryMapper;
    /**
     * 查询商家商品
     * 
     * @param productId 商家商品ID
     * @return 商家商品
     */
    @Override
    public OptShopProduct selectOptShopProductById(String productId)
    {
        return optShopProductMapper.selectOptShopProductById(productId);
    }

    /**
     * 查询商家商品列表
     * 
     * @param optShopProduct 商家商品
     * @return 商家商品
     */
    @Override
    public List<OptShopProduct> selectOptShopProductList(OptShopProduct optShopProduct)
    {
        return optShopProductMapper.selectOptShopProductList(optShopProduct);
    }

    /**
     * 新增商家商品
     * 
     * @param optShopProduct 商家商品
     * @return 结果
     */
    @Override
    public int insertOptShopProduct(OptShopProduct optShopProduct) {
        OptShopCategory optShopCategory = new  OptShopCategory();
        SysUser currentUser  = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String number = RandomStringUtils.randomNumeric(8);
        optShopProduct.setCreateTime(DateUtils.getNowDate());
        optShopCategory.setDeptId(String.valueOf(currentUser.getDeptId()));
        optShopProduct.setProductId(number);
        optShopProduct.setDeptId(String.valueOf(currentUser.getDeptId()));
        optShopCategory.setCategoryNumber(optShopProduct.getCategoryNumber());
        optShopCategory = optShopCategoryMapper.selectProduct(optShopCategory);
        optShopProduct.setCategoryName(optShopCategory.getCategoryName());
        return optShopProductMapper.insertOptShopProduct(optShopProduct);
    }

    /**
     * 修改商家商品
     * 
     * @param optShopProduct 商家商品
     * @return 结果
     */
    @Override
    public int updateOptShopProduct(OptShopProduct optShopProduct)
    {
        optShopProduct.setUpdateTime(DateUtils.getNowDate());
        return optShopProductMapper.updateOptShopProduct(optShopProduct);
    }

    /**
     * 删除商家商品对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteOptShopProductByIds(String ids)
    {
        return optShopProductMapper.deleteOptShopProductByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商家商品信息
     * 
     * @param productId 商家商品ID
     * @return 结果
     */
    @Override
    public int deleteOptShopProductById(String productId)
    {
        return optShopProductMapper.deleteOptShopProductById(productId);
    }
}
